/*  _________________________________________
   /                                      /_ \
  |  			   G.U.I.                |/ \ |
  |                                      |___||
   \______________________________________\_/ |
       |                                      |
      /     G.U.I. Environment               /
     /     Written April, 2009              /
    |     by Darrell Eastman.              |___
    |                                      |   |
     \______________________________________\_/
*/

#include "serverlist.h"
#include "input.h"
#include <irrlicht.h>

using namespace irr;
using namespace gui;
using namespace DataStructures;
using namespace RakNet;

///////////////////// GUI Skins /////////////////////
IEventReceiver	*receiver	= device->getEventReceiver();
IGUIEnvironment	*env	= device->getGUIEnvironment();
IGUISkin			*skin;	// Current Skin
float				*alpha;	// Skin Transparency

//GUI Font, Font Size 8x15
IGUIFont*	GUIFont  = gui.getFont( "media/Kung Fu! Small.bmp" );
IGUIFont*	HUDFont  = gui.getFont( "media/Kung Fu!.bmp" );
IGUIFont*	HUDGlow  = gui.getFont( "media/Kung Fu!.bmp" );

Skin = gui.createSkin( EGST_BURNING_SKIN );
gui.setSkin( Skin );
if( GUIFont.IsValid() = true )
	Skin.setFont( GUIFont );
Skin->setFont( env->getBuiltInFont(), EGDF_TOOLTIP );

//Options (Input, Video, Audio, And Profile)
struct profile
{
	SIrrlichtCreationParameters	Video;
	stringw	Name;
	stringw	Class;
	stringw	Mesh;
	stringw	Skin;
	stringw	Weapon[][];
	stringw	Inventory[][];
	stringw	Input;
	int			View[3];
	bool		Debug;
	list		Lineup;
}

profile.View =
	{12800,					//*TeView Distance
	profile.View[0] / 2,	//Active Distance
	profile.View[1] / 2};	//Interactive Distance

//Player Select

//Server Lobby
//Server Creation
//Client Data Authentication
//Loading Screen
string	MapNam	= "Island";	//Test Value, Map Name               

//Chat Box
//Pause Menu

class UI
{
private:
	string			MenSte;								//Menu State
	IGUIElement*	menu	= env->addGUIElement();	//Menu Element Parent

	// Paused GUI Menu
	bool Pause()
	{
		//Paused Backdrop Template Paused	     
		env->addButton( rect<s32>( res.x/2.66, res.y/1.6, res.x/1.6, res.y/1.41 ), 0, GUI_ID_OPTIONS_BUTTON, L"Options", L"" );
		env->addButton( rect<s32>( res.x/2.66, res.y/1.92, res.x/1.6, res.y/1.66 ), 0, GUI_ID_LOAD_GAME_BUTTON, L"Load Last CheckPoint", L"Screwed up?" );
		env->addButton( rect<s32>( res.x/2.66, res.y/2.4, res.x/1.6, res.y/2 ), 0, GUI_ID_RESUME_BUTTON, L"Resume", L"Back into the game" );
		env->addButton( rect<s32>( res.x/2.66, res.y/1.37, res.x/1.6, res.y/1.23 ), 0, GUI_ID_MAIN_MENU_BUTTON, L"Exit to Menu", L"" );
		env->addButton( rect<s32>( res.x/2.66, res.y/1.2, res.x/1.6, res.y/1.09 ), 0, GUI_ID_QUIT_BUTTON, L"Quit", L"Exit To Unix?" );
		//Option Menu Paused Event Reciever						  
		return true;
	}


	
	// User Statistics Menu
	bool Stats()
	{
		return true;
	}

	
	// User Objective  Menu
	bool Quest()
	{
		return true;
	}

	
	// Update Menu
	Update()
	{
		switch( MenSte )
		{
			case "Introduction":
				break;
				
			case "Loading":
				//Cancel
				//Continue
				break;

			case "Input":
				//Set Input Device
				//Set Device Keys
				break;

			case "Video":
				switch( resx )
				{
					case 800:
						resbox->setSelected( resolutions[0] );
						break;
					case 960:
						resbox->setSelected( resolutions[1] );
						break;
					case 1024:
						if( resy == 600 ) resbox->setSelected( resolutions[2] );
						else resbox->setSelected( resolutions[3] );
						break;
					case 1152:
						resbox->setSelected( resolutions[4] );
						break;
					case 1280:
						if( resy == 720 ) resbox->setSelected( resolutions[5] );
						else if( resy == 854 ) resbox->setSelected( resolutions[7] );
						else if( resy == 960 ) resbox->setSelected( resolutions[8] );
						else resbox->setSelected( resolutions[11] );
						break;
					case 1360:
						resbox->setSelected( resolutions[6] );
						break;
					case 1440:
						if( resy == 900 ) resbox->setSelected( resolutions[9] );
						else resbox->setSelected( resolutions[10] );
						break;
					case 1400:
						resbox->setSelected( resolutions[12] );
						break;
					case 1680:
						resbox->setSelected( resolutions[13] );
						break;
					case 1600:
						resbox->setSelected( resolutions[14] );
						break;
					case 1920:
						if( resy == 1080 ) resbox->setSelected( resolutions[15] );
						else resbox->setSelected( resolutions[17] );
						break;
					case 2048:
						if( resy == 1080 ) resbox->setSelected( resolutions[16] );
						else resbox->setSelected( resolutions[18] );
						break;
					case 2560:
						if( resy == 1600 ) resbox->setSelected( resolutions[19] );
						else resbox->setSelected( resolutions[20] );
						break;
					}
				break;

			case "Lobby":
				receiver->OnEvent( EGET_BUTTON_CLICKED )
				
				switch( )
				{
					//Host
					case "host":
						break;
					//Search
					case "search":
						break:
					//Join
					case "join":
						break:
					//Refresh
					case "refresh":
					default:
				}
				break;

			case "Host":
				break;
		}

		return 0;
	}
	// Create New Menu
	Create()
	{
		dev->getCursorControl()->setVisible( true );
		switch( MenSte )
		{
			case "Introduction":
				// Introduction Menu
				bool		bit32		= driver->getTextureCreationFlag( ETCF_ALWAYS_32_BIT );
				driver->setTextureCreationFlag( ETCF_ALWAYS_32_BIT, false );
				driver->setTextureCreationFlag( ETCF_ALWAYS_16_BIT, true );
				//ITexture*	NinjaLogo	= driver->getTexture( "./dragon.jpg" );
				env->addImage( driver->getTexture( "./dragon.jpg" ), position2d<s32>( 0, 0 ), false, menu );
				env->addButton( rect<s32>( res.x / 2.66, res.y / 1.6, res.x / 1.6, res.y / 1.41 ), menu, GUI_ID_OPTIONS_BUTTON, L"Options", L"" );
				env->addButton( rect<s32>( res.x / 2.66, res.y / 2.4, res.x / 1.6, res.y / 2 ), menu, GUI_ID_SINGLE_PLAYER_BUTTON, L"Single Player Game", L"" );
				env->addButton( rect<s32>( res.x / 2.66, res.y / 1.92, res.x / 1.6, res.y / 1.66 ), menu, GUI_ID_SPLIT_SCREEN_BUTTON, L"Split Screen Game", L"" );
				env->addButton( rect<s32>( res.x / 2.66, res.y / 1.2, res.x / 1.6, res.y / 1.09 ), menu, GUI_ID_QUIT_BUTTON, L"Quit", L"Exit To Unix?" );
				break;		

			case "Input":
				// Input Settings
				break;

			case "Audio": 
				// Audio Settings
				rect<s32>			rect	= rect<s32>( 0, 0, 1280, 960 );
				env->addStaticText( L"Volume", rect<s32>( res.x / 6, res.y / 36, res.x / 6 + res.x / 2, res.y / 36 + res.y / 27 ));
				IGUIScrollBar		*music	= env->addScrollBar( true, rect<s32>( res.x * 0.166, res.y * 0.06, res.x * 0.166 + res.x * 0.2, res.y * 0.06 + res.y * 0.02 ), null, GUI_ID_MUSIC_SCROLL_BAR );
				env->addStaticText( L"Music", rect<s32>( res.x * 0.166, res.y * 0.09, res.x * 0.166 + res.x * 0.2, res.y * 0.09 + res.y * 0.02 ));
				IGUIScrollBar		*master	= env->addScrollBar( true, rect<s32>( res.x * 0.38, res.y * 0.06, res.x * 0.38 + res.x * 0.2, res.y * 0.06 + res.y * 0.02 ), null, GUI_ID_MASTER_VOL_SCROLL_BAR );
				env->addStaticText( L"Master", rect<s32>( res.x * 0.38, res.y * 0.09, res.x * 0.38 + res.x * 0.2, res.y * 0.09+res.y * 0.02 ));
				music->setPos( Music_Volume * 100 );
				master->setPos( Master_Volume * 100 );
				break;

			case "Video":
				// Video Settings

				IGUICheckBox		*bloom;
				IGUICheckBox		*HDR;
				IGUICheckBox		*none;

				rect<s32>			rect				= rect<s32>( 0, 0, 1280, 960 );
				EventRec.BLOOM		= bloom				= env->addCheckBox( Bloom_enabled, rect<s32>( res.x / 6, res.y / 15, res.x / 6 + res.x / 6, res.y / 15 + res.y / 27 ), 0, GUI_ID_BLOOM, L" Bloom" );
				EventRec.HDR		= HDR				= env->addCheckBox( HDR_enabled, rect<s32>( res.x / 3, res.y / 15, res.x / 3 + res.x / 6, res.y / 15 + res.y / 27 ), null, GUI_ID_HDR, L" HDR" );
				EventRec.NO_HDR	= none				= env->addCheckBox( !( HDR_enabled||Bloom_enabled ), rect<s32>( res.x / 1.5, res.y / 15, res.x / 1.5 + res.x / 6, res.y / 15 + res.y / 27 ), 0, GUI_ID_NO_HDR, L" No Image Enchancing" );
				IGUIListBox		*resbox				= env->addListBox( rect<s32>( res.x * 0.8, res.y * 0.2, res.x * 0.8 + res.x * 0.19, res.y * 0.2 + res.y * 0.1405 ), null, GUI_ID_RES_LISTBOX );
				u32					resolutions[21]	= {resbox->addItem( L"800x600" ), resbox->addItem( L"960x540" ), resbox->addItem( L"1024x600" ), resbox->addItem( L"1024x768" ), resbox->addItem( L"1152x864" ), resbox->addItem( L"HD 1280x720" ), resbox->addItem( L"1360x768" ), resbox->addItem( L"1280x854" ), resbox->addItem( L"1280x960" ), resbox->addItem( L"1440x900" ), resbox->addItem( L"1440x960" ), resbox->addItem( L"1280x1024" ), resbox->addItem( L"1400x1050" ), resbox->addItem( L"1680x1050" ), resbox->addItem( L"1600x1200" ), resbox->addItem( L"TrueHD 1920x1080" ), resbox->addItem( L"2K 2048x1080" ), resbox->addItem( L"1920x1200" ), resbox->addItem( L"2048x1536" ), resbox->addItem( L"2560x1600" ), resbox->addItem( L"2560x2048" )};
				env->addStaticText( L"Image Enrichment", rect<s32>( res.x / 6, res.y / 36, res.x / 6 + res.x / 2, res.y / 36 + res.y / 27 ));
				env->addCheckBox( DOF_enabled, rect<s32>( res.x / 6, res.y / 9, res.x / 6+res.x / 6, res.y / 9 + res.y / 27 ), null, GUI_ID_DOF, L" Depth Of Field" );
				env->addStaticText( L"General Graphics", rect<s32>( res.x * 0.01, res.y * 0.17, res.x * 0.01 + res.x / 2, res.y * 0.17 + res.y * 0.0405 ));
				env->addCheckBox( fullscreen, rect<s32>( res.x * 0.01, res.y * 0.2, res.x * 0.01+res.x * 0.15, res.y * 0.2 + res.y * 0.0405 ), null, GUI_ID_FULLSCREEN, L" Fullscreen" );
				env->addCheckBox( stencilbuffer, rect<s32>( res.x * 0.17, res.y * 0.2, res.x * 0.17 + res.x * 0.2, res.y * 0.2 + res.y * 0.0405 ), null, GUI_ID_STENCIL_BUFFER, L" Stencil Buffer ( shadows )" );
				env->addCheckBox( vsync_enabled, rect<s32>( res.x * 0.4, res.y * 0.2, res.x * 0.4 + res.x * 0.15, res.y * 0.2 + res.y * 0.0405 ), null, GUI_ID_VSYNC, L" Vsync" );
				env->addCheckBox( AA, rect<s32>( res.x * 0.6, res.y * 0.2, res.x * 0.6 + res.x * 0.17, res.y * 0.2 + res.y * 0.0405 ), null, GUI_ID_ANTIALIAS, L" Anti Aliasing" );
				break;

			case "Profile":
				// User Profile Settings
				//video
				//name
				//skill
				//skin
				//mesh
				//weapon
				//item
				//input
				break;

			case "Lobby":
				// Server Lobby
				env->addStaticText( L"Swashbuckler ho! - Server Lobby", rect<s32>(  32,   8, 456,  56 ), false, false, menu );
				IGUIListBox	*name		= env->addListBox( rect<s32>(  32, 96, 272, 581 ), menu );
				IGUIListBox	*ping		= env->addListBox( rect<s32>( 272, 96, 336, 581 ), menu );
				IGUIListBox	*players	= env->addListBox( rect<s32>( 336, 96, 440, 581 ), menu );
				IGUIListBox	*map			= env->addListBox( rect<s32>( 440, 96, 600, 581 ), menu );
				IGUIListBox	*mode		= env->addListBox( rect<s32>( 600, 96, 744, 581 ), menu );
				IGUIButton		*nametab	= env->addButton( rect<s32>( 032, 064, 272, 096 ), menu, -1, L"Server Name" );
				IGUIButton		*pingtab	= env->addButton( rect<s32>( 272, 064, 336, 096 ), menu, -1, L"Ping" );
				IGUIButton		*playertab	= env->addButton( rect<s32>( 336, 064, 440, 096 ), menu, -1, L"Players" );
				IGUIButton		*maptab		= env->addButton( rect<s32>( 440, 064, 600, 096 ), menu, -1, L"Map Name" );
				IGUIButton		*modetab	= env->addButton( rect<s32>( 600, 064, 744, 096 ), menu, -1, L"Game Mode" );
				IGUIButton		*search		= env->addButton( rect<s32>( 296, 592, 409, 625 ), menu, -1, L"Search" );
				IGUIButton		*host		= env->addButton( rect<s32>( 424, 592, 537, 625 ), menu, -1, L"Host" );
				IGUIButton		*join		= env->addButton( rect<s32>( 552, 592, 665, 625 ), menu, -1, L"Join" );
				IGUIButton		*refresh	= env->addButton( rect<s32>( 680, 592, 793, 625 ), menu, -1, L"Refresh" );
				IGUIScrollBar	*scroll		= env->addScrollBar( false, rect<s32>( 744, 96, 776, 576 ), menu );
				scroll->setMax();
				env->addStaticText( L"Swashbuckler ho! v0.1", rect<s32>( 32, 608, 145, 625 ), false, false, menu );
				break;

			case "Host":
				// Server Settings
				break;

			case "Loading":
				// Map Loading
				driver->setTextureCreationFlag( ETCF_ALWAYS_32_BIT, bit32 );
				driver->setTextureCreationFlag( ETCF_ALWAYS_16_BIT, false );
				//ITexture*	Loading		= driver->getTexture( "./load.png" );
				env->addImage( driver->getTexture( "./load.png" ), position2d<s32>( 0, 0 ), false, menu );
				env->addStaticText( L"Swashbuckler ho! Loading...", rect<s32>( 16, 8, 528, 40 ), false, false, menu );
				env->addStaticText( L"Map Name...\n	" + MapNam, rect<s32>( 56, 56, 528, 88 ), false, false, menu );
				env->addStaticText( L"Description...\n	" + GamMode, rect<s32>( 16, 296, 528, 408 ), false, false, menu );
				IGUIStaticText	*imag	= env->addStaticText( L"Map Image...", rect<s32>( 16, 104, 528, 408 ), false, false, menu );
				IGUIButton			*accept	= env->addButton( rect<s32>( 288, 424, 401, 457 ), menu, -1, L"Accept" );
				IGUIButton			*cancel	= env->addButton( rect<s32>( 416, 424, 529, 457 ), menu, -1, L"Cancel" );
				break;
				
			default:
		}

		return 1;
	}
	Remove()
	{
		menu->remove();	//Remove Parent Element
		dev->getCursorControl()->setVisible( false );
		
		return 0;
	}
};